package cz.muni.fi.pv168.addressbook.entities;

import cz.muni.fi.pv168.addressbook.entities.xml.LocalDateAdapter;
import java.time.LocalDate;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 *
 * @author Michal Ďuriš
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Person {

    @XmlAttribute(required = true)
    private Long id;

    @XmlElement
    private String firstName;

    @XmlElement(required = true)
    private String lastName;

    @XmlElement
    private String note;

    @XmlElement
    @XmlJavaTypeAdapter(LocalDateAdapter.class)
    private LocalDate birthDate;

    public Person() {
    }

    public Person(String firstName, String lastName) {

        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Person(String firstName, String lastName, String note,
                  LocalDate birthDate) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.note = note;
        this.birthDate = birthDate;
    }

    public void setPerson(Person person) {

        id = person.getId();

        firstName = person.getFirstName();
        lastName = person.getLastName();
        note = person.getNote();

        birthDate = person.getBirthDate();
    }

    public LocalDate getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(LocalDate birthDate) {
        this.birthDate = birthDate;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @Override
    public String toString() {
        return lastName + " " + firstName;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        final Person other = (Person) obj;

        return Objects.equals(this.id, other.id);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 13 * hash + Objects.hashCode(this.id);
        return hash;
    }

}
